package com.core.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Element;

public class ConfigurableConstants
{
  protected static Log logger = LogFactory.getLog(ConfigurableConstants.class);
  protected static Properties p = new Properties();
  protected static Element root;
  public static Date validFrom;
  public static Date flightFrom;
  public static String oldVersion;
  public static String newVersion;
  public static String czafpFareinfo1;
  public static String czafpFareinfo2;
  
  static {
//    init("config.properties");
    init("hotel.properties");
//    initXML();
//    initVersion();
  }

/*  protected static void initXML(){
		String fileName = ConfigurableConstants.getProperty("brandSequences.xmlName", "brandSequences.xml");

		URL url= Thread.currentThread().getContextClassLoader().getResource(fileName);
		File f = new File(url.getFile());
		
		SAXReader reader = new SAXReader();
		Document doc = null;
		try {
			doc = reader.read(f);
		} catch (DocumentException e) {
			logger.error("read xml error,please check it!  " + fileName);
		}
		 root = doc.getRootElement();
  };*/
  
 /* protected static void initVersion() {
	// TODO Auto-generated method stub
	  	Properties properties = new Properties();
		String filePath = "brandTransition.properties";
		Resource resource = new ClassPathResource(filePath);
		try {
			properties.load(resource.getInputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String valid = properties.getProperty("brand.saleDate").trim();
		String flight = properties.getProperty("brand.flightDate").trim();
		oldVersion = properties.getProperty("brand.oldVersion").trim();
		newVersion = properties.getProperty("brand.newVersion").trim();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			validFrom = sf.parse(valid);
			flightFrom = sf.parse(flight);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		czafpFareinfo1=p.getProperty("czafp.fareinfo1");
		czafpFareinfo2=p.getProperty("czafp.fareinfo2");
  }*/

protected static void init(String propertyFileName)
  {
    InputStream in = null;
    try
    {
      in = ConfigurableConstants.class.getClassLoader().getResourceAsStream(propertyFileName);
      if (in != null)
      {
        p.load(in);
      }
    } catch (IOException e) {
      logger.error("load " + propertyFileName + " into Constants error!");

      if (in != null)
        try {
          in.close();
        } catch (IOException e1) {
          logger.error("close " + propertyFileName + " error!");
        }
    }
    finally
    {
      if (in != null)
        try {
          in.close();
        } catch (IOException e) {
          logger.error("close " + propertyFileName + " error!");
        }
    }
  }

  
  /**
   * 根据key获取Properties value
   * @param str
   * @param deafultValue
   * @return
   */
  public static String getProperty(String key, String defaultValue)
  {
    return p.getProperty(key, defaultValue);
  }
}